import 'package:architecture/os_size.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../db/isar.dart';
import '../../widgets/text_button.dart';
import 'relax_controller.dart';

class RelaxPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetX<RelaxController>(
        init: RelaxController(),
        builder: (controller) {
          return Scaffold(
            appBar: AppBar(
              title: Text('放松一会儿'),
              actions: [
                TextButton(
                    onPressed: () {
                      controller.addRecord();
                    },
                    child: Text(
                      '添加记录',
                      style: TextStyle(color: Colors.white),
                    )),
              ],
            ),
            body: _relaxList(controller),
          );
        });
  }

  _relaxList(RelaxController controller) {
    return Container(
        width: windowWidth * 0.8,
        margin: EdgeInsets.only(top: windowHeight * 0.01),
        child: ListView.builder(
          itemCount: controller.relaxInfoList.length,
          itemBuilder: (context, index) {
            final item = controller.relaxInfoList[index];
            return _itemLayout(item, controller);
          },
        ));
  }

  _itemLayout(RelaxInfo relaxInfo, RelaxController controller) {
    double globPositionX = 0;
    double globPositionY = 0;
    return Listener(
        onPointerMove: (movePointEvent) {},
        child: GestureDetector(
            onTap: () {},
            onLongPress: () {},
            onPanDown: (DragDownDetails details) {
              details.globalPosition.dx - 5;
              globPositionY = details.globalPosition.dy + 10;
            },
            child: Container(
                alignment: Alignment.centerLeft,
                height: 50,
                width: windowWidth * 0.15,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    Text(
                      relaxInfo.date,
                      style: TextStyle(
                          color: Colors.blueAccent,
                          fontWeight: FontWeight.bold),
                    ),
                    Row(
                      children: [
                        IgnorePointer(
                            ignoring: relaxInfo.state == 1,
                            child: IconButton(
                                onPressed: () {
                                  controller.updateRecord(relaxInfo,
                                      isAdd: false);
                                },
                                icon: Icon(
                                  Icons.remove,
                                  size: 20,
                                  color: relaxInfo.state == 1
                                      ? Colors.grey
                                      : Colors.deepPurpleAccent,
                                ))),
                        Text(
                          relaxInfo.count.toString(),
                          style: TextStyle(color: Colors.redAccent),
                        ),
                        IgnorePointer(
                            ignoring: relaxInfo.state == 1,
                            child: IconButton(
                              onPressed: () {
                                controller.updateRecord(relaxInfo);
                              },
                              icon: Icon(Icons.add),
                              color: relaxInfo.state == 1
                                  ? Colors.grey
                                  : Colors.deepPurpleAccent,
                            )),
                      ],
                    ),
                    Row(
                      children: [
                        IgnorePointer(
                            ignoring: relaxInfo.state == 1,
                            child: _textButton('完成',
                                color: relaxInfo.state == 1
                                    ? Colors.grey
                                    : Colors.green, onPressed: () {
                              relaxInfo.state = 1;
                              controller.finish(relaxInfo);
                            })),
                        SizedBox(
                          width: 15,
                        ),
                        _textButton('删除', color: Colors.redAccent,
                            onPressed: () {
                          controller.deleteRecord(relaxInfo.date);
                        }),
                      ],
                    )
                  ],
                ))));
  }

  _textButton(String text,
      {Function? onPressed, Color color = Colors.deepPurpleAccent}) {
    return Container(
        alignment: Alignment.center,
        child: MyTextButton(
            text: text,
            confirm: () {
              onPressed?.call();
            },
            backgroundColor: color,
            sideColor: color,
            textStyle: TextStyle(color: Colors.white)));
  }
}
